package dao.productDao;

import dao.DaoException;
import model.Product;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ProductDaoImpl implements ProductDao {
    @Override
    public boolean addProduct(Product product) throws DaoException {
        return false;
    }

    @Override
    public Product findById(int id) throws DaoException {

        String sql = "SELECT * FROM products WHERE id = ?";

        Product product = new Product();
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
        ) {
            pstmt.setInt(1, id);
            try (ResultSet resultSet = pstmt.executeQuery()) {
                if (resultSet.next()) {
                    product.setPid(resultSet.getInt("id"));
                    product.setPname(resultSet.getString("pname"));
                    product.setPrice(resultSet.getDouble("price"));
                    product.setStock(resultSet.getInt("stock"));
                    product.setType(resultSet.getString("type"));
                    product.setImgURL(resultSet.getString("imgURL"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        return product;
    }

    @Override
    public ArrayList<Product> findProductsByType(String type) throws DaoException {
        ArrayList<Product> products = new ArrayList<>();

        String sql = "SELECT * FROM products WHERE type = ?";
        try (Connection conn = getConnection();
             PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ) {
            preparedStatement.setString(1, type);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Product product = new Product();

                product.setPid(resultSet.getInt("id"));
                product.setPname(resultSet.getString("pname"));
                product.setPrice(resultSet.getDouble("price"));
                product.setStock(resultSet.getInt("stock"));
                product.setType(resultSet.getString("type"));
                product.setImgURL(resultSet.getString("imgURL"));
                products.add(product);
            }
            return products;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public ArrayList<Product> findProductsByPattern(String pattern) throws DaoException {
        pattern = "%" + pattern + "%";

        ArrayList<Product> products = new ArrayList<>();

        String sql = "SELECT * FROM products WHERE pname LIKE ?";
        try (Connection conn = getConnection();
             PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ) {
            preparedStatement.setString(1, pattern);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Product product = new Product();

                product.setPid(resultSet.getInt("id"));
                product.setPname(resultSet.getString("pname"));
                product.setPrice(resultSet.getDouble("price"));
                product.setStock(resultSet.getInt("stock"));
                product.setType(resultSet.getString("type"));
                product.setImgURL(resultSet.getString("imgURL"));
                products.add(product);
            }
            return products;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean updateStockById(int id, int quantity) throws DaoException {
        String sql = "UPDATE products SET stock=stock-? WHERE id=?";
        boolean success = false;
        try (Connection conn = getConnection();
             PreparedStatement preparedStatement = conn.prepareStatement(sql)
        ) {
            preparedStatement.setInt(1, quantity);
            preparedStatement.setInt(2, id);
            success = preparedStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return success;
    }
}
